import 'package:flutter_app/generated/json/base/json_convert_content.dart';
import 'package:flutter_app/net_data/base/api_client.dart';
import 'package:flutter_app/net_data/entity/banner_entity.dart';
import 'package:flutter_app/util/DialogUtil.dart';
import 'package:flutter_app/util/oktoast.dart';

import '../api.dart';

class HttpClient extends IApiClient {
  // 静态私有成员，没有初始化
  static HttpClient? _instance;

  // 工厂模式 : 单例公开访问点
  factory HttpClient() {
    return _getInstance();
  }

  // 静态、同步、私有访问点
  static HttpClient _getInstance() {
    if (_instance == null) {
      _instance = HttpClient._internal();
    }
    return _instance!;
  }

  HttpClient._internal();

  @override
  void dealLoading(bool show) {
    if (show) {
      DialogUtil.showLoadingDialog();
    } else {
      DialogUtil.closeLoadingDialog();
    }
  }

  @override
  void dealNeedLogin() {
    ///TODO:通过eventbus传递登录逻辑
  }

  @override
  String getBaseUrl() {
    return Api.HOST_TEST;
  }

  @override
  bool needEncrypt() {
    return false;
  }

  @override
  bool needLog() {
    return true;
  }

  @override
  void showToastMsg(String msg) {
    ToastUtils.toast(msg);
  }

  @override
  String userToken() {
    return "uid";
  }

  ///TODO:模拟接口数据
  void getRepairTaskList(int pageNo, int repairStatus,
      {Function? onSuccess, Function? onError}) {
    Map<String, dynamic> map = {
      "pageNo": pageNo,
      "pageSize": 20,
      "repairStatus": repairStatus
    };
    getRequest(Api.TASK_LIST, params: map, success: (dataJson) {
      BannerEntity model = JsonConvert.fromJsonAsT(dataJson);
      if (onSuccess != null) {
        onSuccess(model);
      }
    }, error: (code, msg) {
      if (onError != null) {
        onError(code, msg);
      }
    });
  }
}
